"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/17 15:04
@Description :  线程死锁

"""
import threading

list_num = [1, 2, 3, 4, 5]

lock = threading.Lock()


def get_num(index):
    # 加锁     同一个时间只能有一个线程操作列表
    lock.acquire()
    try:
        result = list_num[index]
        print(result)
    except IndexError as e:
        print(e)
        lock.release()
    else:
        # 解锁
        lock.release()


if __name__ == '__main__':
    for i in range(10):  # 0 1 2 3 4
        threading.Thread(target=get_num, args=(i,)).start()
